package club.baimahu.pencil.domain.entity.discount;

import club.baimahu.pencil.domain.entity.order.CommodityList;

import java.math.BigDecimal;

/**
 * 按固定金额减免
 */
public class FixDiscountStrategy  implements DiscountStrategy{

    private BigDecimal deducting;

    private Condition condition;

    public FixDiscountStrategy(BigDecimal deducting){
        this.deducting = deducting;
    }

    public FixDiscountStrategy(BigDecimal deducting,Condition condition){
        this.deducting = deducting;
        this.condition = condition;
    }

    @Override
    public BigDecimal discount(CommodityList commodityList) {
        BigDecimal originalTotal = commodityList.totalMoney();
        if(condition == null || condition.fit(commodityList)){
            BigDecimal result =  originalTotal.subtract(deducting);
            if(result.doubleValue() < 0){
                return BigDecimal.ZERO;
            }else{
                return result;
            }

        }else{
            return originalTotal;
        }
    }

    @Override
    public void setCondition(Condition condition) {
        this.condition = condition;
    }
}
